package nl.windgazer.arachne.components.slideshow;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.AfterRenderTemplate;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.SupportsInformalParameters;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.InitializationPriority;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

@SupportsInformalParameters
@Import(library={"classpath:/nl/windgazer/jsrepository/Events.js","classpath:/nl/windgazer/jsrepository/LinkListener.js","Show.js"}, stylesheet={"Show.css"})
public class Show {

	//Injects and Persists

	@Inject
	private ComponentResources resources;

	@Inject
	private JavaScriptSupport jss;

	//Properties and Parameters

	private String id = null;

	@Parameter(required = true, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "width")
	private float _width;

	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "offset")
	private float _offset = new Float(0.5);
	
	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "padding")
	private float _padding = 1;

	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "unit")
	private String _unit = "em";

	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "duration")
	private String _duration = "1000ms";

	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "autorun")
	private int _autorun = -1;

	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "transition")
	private String _transition = "margin,opacity";
	
	@Parameter(required = false, principal = false, defaultPrefix = BindingConstants.LITERAL, name = "timing")
	private String _timing = "ease-out";

	//Components

	//Events

	@AfterRenderTemplate
	public void attachJSInit() {
		jss.addInitializerCall(
				InitializationPriority.LATE, "ArachneSlideShow"
				, new JSONObject(
						"id", getId()
						, "width", Float.toString(_width)
						, "offset", Float.toString(_offset)
						, "padding", Float.toString(_padding)
						, "unit", _unit
						, "duration", _duration
						, "autorun", Integer.toString(_autorun)
						, "transition", _transition
						, "timing", _timing
				)
		);
	}

	//Other (check for business-logic, it should not remain here)

	public String getId() {
		if (id==null) {
			id = resources.getId();
		}
		return id;
	}

	public String getClassName() {
		final StringBuilder sb = new StringBuilder();
		sb.append(resources.getInformalParameter("class", String.class));
		sb.append(" lbcss3 ");
		sb.append(_transition.replace(',', ' '));
		return sb.toString();
	}
}
